package atri.mapper;

import atri.model.Book;
import atri.model.Recommend;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface BookMapper extends BaseMapper<Book> {
    @Delete("DELETE FROM book WHERE father_id = #{f_id}")
    int delete_under_comments(int f_id);

    @Select("SELECT * FROM book WHERE father_id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "auther", property = "auther"),
            @Result(column = "content", property = "content"),
            @Result(column = "stars", property = "stars"),
            @Result(column = "time", property = "time"),
            @Result(column = "img_url", property = "img_url"),
            @Result(column = "category", property = "category"),
            @Result(column = "view_sum", property = "view_sum"),
    })
    List<Book> find_f_comment(int id);
    @Select("select * from book where name LIKE '%${name}%'")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "auther", property = "auther"),
            @Result(column = "content", property = "content"),
            @Result(column = "stars", property = "stars"),
            @Result(column = "time", property = "time"),
            @Result(column = "img_url", property = "img_url"),
            @Result(column = "category", property = "category"),
            @Result(column = "view_sum", property = "view_sum"),
    })
    List<Book> find_by_content(String name);

    @Select("select * from book where user_id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "auther", property = "auther"),
            @Result(column = "content", property = "content"),
            @Result(column = "stars", property = "stars"),
            @Result(column = "time", property = "time"),
            @Result(column = "img_url", property = "img_url"),
            @Result(column = "category", property = "category"),
            @Result(column = "view_sum", property = "view_sum"),
    })
    List<Book> find_by_user(int id);

    @Select("select * from book where id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
    })
    Book find_name(int id);

    @Delete("DELETE FROM suer WHERE user_id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "auther", property = "auther"),
            @Result(column = "content", property = "content"),
            @Result(column = "stars", property = "stars"),
            @Result(column = "time", property = "time"),
            @Result(column = "img_url", property = "img_url"),
            @Result(column = "category", property = "category"),
            @Result(column = "view_sum", property = "view_sum"),
    })
    Boolean delete_by_user_id(int id);
    @Update("UPDATE book SET view_sum = view_sum + 1 WHERE id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "user_id"),
            @Result(column = "title", property = "title"),
            @Result(column = "content", property = "content"),
            @Result(column = "time", property = "time"),
            @Result(column = "star", property = "star"),
            @Result(column = "view_sum", property = "view_sum"),
            @Result(column = "img_url", property = "img_url")
    })
    Boolean add_view(int id);

    @Update("UPDATE book SET stars = (stars * 0.8) + (#{rate} * 0.2) WHERE id = #{id}")
    Boolean stars_new(float rate,int id);

    @Select("select * from book where id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "user_id"),
            @Result(column = "title", property = "title"),
            @Result(column = "content", property = "content"),
            @Result(column = "time", property = "time"),
            @Result(column = "star", property = "star"),
            @Result(column = "view_sum", property = "view_sum"),
            @Result(column = "img_url", property = "img_url")
    })
    Book get_a_book(int id);

    @Select("select * from book where category = #{category}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "user_id"),
            @Result(column = "title", property = "title"),
            @Result(column = "content", property = "content"),
            @Result(column = "time", property = "time"),
            @Result(column = "star", property = "star"),
            @Result(column = "view_sum", property = "view_sum"),
            @Result(column = "img_url", property = "img_url")
    })
    List<Book> find_by_category(String category);

}
